前言
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
安装启动
# 下载文件 |
基本使用
默认链接的是test库,mongo是安装时自带的客户端命令。./mongo
MongoDB shell version: 3.0.6
connecting to: test
> db.student.insert({'id':10001, 'name':'Jeck', 'age': 25, 'address':'China', 'phoneNo':'13243744257'})
WriteResult({ "nInserted" : 1 })
> db.student.find()
{ "_id" : ObjectId("5b65b06d1e371899dbc900b9"), "id" : 10001, "name" : "Jeck", 'age': 25, "address" : "China", "phoneNo" : "13243744257" }
{ "_id" : ObjectId("5b65b0a81e371899dbc900ba"), "id" : 10002, "name" : "rouse", 'age': 20, "address" : "Jeban", "phoneNo" : "13243744278" }
> db.student.find({'address':'China'})
{ "_id" : ObjectId("5b65b06d1e371899dbc900b9"), "id" : 10001, "name" : "Jeck", "address" : "China", "phoneNo" : "13243744257" }
# 查询匹配20<age<30的记录
> db.student.find({'age':{$gt:20, $lt:30}})
{ "_id" : ObjectId("5b65b2ab1e371899dbc900bd"), "id" : 10001, "name" : "Jeck", "age" : 25, "address" : "China", "phoneNo" : "13243744257" }
# 统计符合条件的总数
> db.student.find({'age':{$gt:20, $lt:30}}).count()
2
# 1:升序
> db.student.find({'age':{$gt:20, $lt:30}}).sort({'age':1})
{ "_id" : ObjectId("5b65b4081e371899dbc900c0"), "id" : 10004, "name" : "rouse4", "age" : 22, "address" : "Jeban", "phoneNo" : "13543744898" }
{ "_id" : ObjectId("5b65b2ab1e371899dbc900bd"), "id" : 10001, "name" : "Jeck", "age" : 25, "address" : "China", "phoneNo" : "13243744257" }
# -1:降序
> db.student.find({'age':{$gt:20, $lt:30}}).sort({'age':-1})
{ "_id" : ObjectId("5b65b2ab1e371899dbc900bd"), "id" : 10001, "name" : "Jeck", "age" : 25, "address" : "China", "phoneNo" : "13243744257" }
{ "_id" : ObjectId("5b65b4081e371899dbc900c0"), "id" : 10004, "name" : "rouse4", "age" : 22, "address" : "Jeban", "phoneNo" : "13543744898" }
# 分页查询
> db.student.find({'age':{$gt:20, $lt:30}}).sort({'age':1}).skip(1).limit(1)
{ "_id" : ObjectId("5b65b2ab1e371899dbc900bd"), "id" : 10001, "name" : "Jeck", "age" : 25, "address" : "China", "phoneNo" : "13243744257" }
# 模糊查询:匹配前缀以rouse开头的name
> db.student.find({'name':/^rouse/})
{ "_id" : ObjectId("5b65b2c11e371899dbc900be"), "id" : 10001, "name" : "rouse", "age" : 20, "address" : "Jeban", "phoneNo" : "13243744897" }
{ "_id" : ObjectId("5b65b3f31e371899dbc900bf"), "id" : 10003, "name" : "rouse3", "age" : 30, "address" : "Jeban", "phoneNo" : "13243744898" }
{ "_id" : ObjectId("5b65b4081e371899dbc900c0"), "id" : 10004, "name" : "rouse4", "age" : 22, "address" : "Jeban", "phoneNo" : "13543744898" }
{ "_id" : ObjectId("5b65b7e13f43e28fb79f732f"), "id" : 10005, "name" : "rouse 5", "age" : 25, "address" : "Jeban", "phoneNo" : "13453744898" }
# 仅更新匹配的第一条
> db.student.update({'name':/^rouse/, 'age':{$gt:22}}, {$set:{'address':'Thailand'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
# 更新所有匹配的记录
> db.student.update({'name':/^rouse/, 'age':{$gt:22}}, {$set:{'address':'Thailand'}}, {multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })
# 移除匹配的记录
> db.student.remove({'name':'Jeck'})
WriteResult({ "nRemoved" : 1 })
# 移除所有记录
> db.student.remove({})
WriteResult({ "nRemoved" : 2 })
索引操作:(1:升序创建索引,-1:降序创建索引)# 单列索引:指定name升序创建索引
> db.student.createIndex({'name':1})
2018-08-04T10:36:41.824-0400 I INDEX [conn7] build index on: test.student properties: { v: 1, key: { name: 1.0 }, name: "name_1", ns: "test.student" }
2018-08-04T10:36:41.824-0400 I INDEX [conn7] building index using bulk method
2018-08-04T10:36:41.824-0400 I INDEX [conn7] build index done. scanned 5 total records. 0 secs
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
# 联合索引
> db.student.createIndex({'id':1, 'age':-1})
2018-08-04T10:40:17.782-0400 I INDEX [conn7] build index on: test.student properties: { v: 1, key: { id: 1.0, age: -1.0 }, name: "id_1_age_-1", ns: "test.student" }
2018-08-04T10:40:17.782-0400 I INDEX [conn7] building index using bulk method
2018-08-04T10:40:17.782-0400 I INDEX [conn7] build index done. scanned 5 total records. 0 secs
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
# 唯一索引
> db.student.createIndex({'phoneNo':1}, {unique:true})
2018-08-04T10:42:33.740-0400 I INDEX [conn7] build index on: test.student properties: { v: 1, unique: true, key: { phoneNo: 1.0 }, name: "phoneNo_1", ns: "test.student" }
2018-08-04T10:42:33.740-0400 I INDEX [conn7] building index using bulk method
2018-08-04T10:42:33.740-0400 I INDEX [conn7] build index done. scanned 5 total records. 0 secs
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
数据库操作:
# 显示所有数据库 |
参考链接